package com.start_eternity.oasissafewalk.controller;

import com.start_eternity.oasissafewalk.comment.ErrorCode;
import com.start_eternity.oasissafewalk.comment.ResultUtils;
import com.start_eternity.oasissafewalk.exception.BusinessException;
import com.start_eternity.oasissafewalk.pojo.Notice;
import com.start_eternity.oasissafewalk.comment.Result;
import com.start_eternity.oasissafewalk.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * author:ZhangT
 * Date:2024 / 01 / 31 / 19:10
 * Description:
 */
@Slf4j
@RestController
@RequestMapping("notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    //查找公告所有数据
    @GetMapping("checkAll")
    public Result<List<Notice>> check() {
        List<Notice> notices = noticeService.findAll();
        if (!notices.isEmpty()) {
            return ResultUtils.success(notices);
        }
        return ResultUtils.error(ErrorCode.NO_DATA);
    }

    //分页查询
    @GetMapping
    public Result<List<Notice>> findPages(@RequestParam(required = false,defaultValue = "1") Integer page,
                                  @RequestParam(required = false,defaultValue = "10")Integer pageSize) {
        List<Notice> notices = noticeService.queryPage(page, pageSize);
        if (! notices.isEmpty()) {
            return ResultUtils.success(notices);
        }
        return ResultUtils.error(ErrorCode.NO_DATA);
    }

    //根据id删除公告
    @DeleteMapping("/deleteBachId/{nId}")
    public Result<Integer> delete(@PathVariable List<Integer> nId){
        log.info("根据id删除信息:{}",nId);
        int i = noticeService.delBatchId(nId);
        if (i > 0) {
            return ResultUtils.success(i);
        }
        return ResultUtils.error(ErrorCode.FAIL_ID);
    }

    //根据标题删除公告
    @DeleteMapping("/deleteByTitle/{title}")
    public Result<Integer> delete(@PathVariable String title){
        log.info("根据标题删除公告:{}",title);
        int i = noticeService.delByNTitling(title);
        if (i > 0) {
            return ResultUtils.success(i);
        }
        return ResultUtils.error(ErrorCode.FAIL_DELETE);
    }

    //根据标题查询公告
    @GetMapping("/checkByTitle")
    public Result<List<Notice>> queryByTitle(@RequestParam String nTitle) {
        List<Notice> notices = noticeService.queryByTitle(nTitle);
        if (!notices.isEmpty()) {
            return ResultUtils.success(notices);
        }
        return ResultUtils.error(ErrorCode.FAIL_TITLE);
    }

    //根据id查询公告
    @GetMapping("/checkById")
    public Result<List<Notice>> check(@RequestParam Integer nId) {
        List<Notice> notices = noticeService.queryAllByNIding(nId);
        if (!notices.isEmpty()) {
            return new Result<>(ErrorCode.SUCCESS,notices);
        }
        return ResultUtils.error(ErrorCode.FAIL_ID);
    }

    //批量添加公告
    @PostMapping("/save")
    public Result<Integer> save(@RequestBody List<Notice> notices){
        log.info("新增公告,notices:{}",notices);
        int i = noticeService.insertBatching(notices);
        if (i > 0) {
            return ResultUtils.success(i);
        }
         throw new BusinessException(ErrorCode.FAIL_INSERT);
    }

    //更新公告
    @PutMapping("/update")
    public Result<Integer> update(@RequestBody Notice notice){
        log.info("更新公告信息:{}",notice);
        int i = noticeService.updateByNIding(notice);
        if (i > 0) {
            return ResultUtils.success(i);
        }
        return ResultUtils.error(ErrorCode.FAIL_UPDATE);
    }
}